#include "QtStra_Xml.h"

extern char* gUser;

QtStraXml::QtStraXml(char* filename)
{
	m_filename = filename;

	m_bvalid = false;

	InitXmlParse(m_filename.c_str());
		
	InitRowAndColCnt();
	
}

QtStraXml::~QtStraXml()
{
	if (m_doc)
	{
		delete m_doc;
	}
	
}


void QtStraXml::InitXmlParse(const char* filename)
{
	// ----------------------1.Lodad file---------------------------------------------------------------
	m_doc = new TiXmlDocument( filename );
	bool loadOkay = m_doc->LoadFile();

	if ( !loadOkay )
	{
		pantheios::log_ERROR("[InitXmlParse] ", filename, "--Could not load Error:", "doc.ErrorDesc()");
		return;
	}
	// ---------------------------------------------------------------------------------------------------

	// ----------------------2.Judge gUser---------------------------------------------------------------
	
	TiXmlElement* root = m_doc->RootElement();

	if (NULL == root)
	{
		pantheios::log_ERROR("[InitXmlParse] ", filename, "root NULL");
		return;
	}




	m_property = root->FirstChildElement();//property
	
	if (NULL == root)
	{
		pantheios::log_ERROR("[InitXmlParse] ", filename, "m_property NULL");
		return;
	}



	TiXmlElement* Users = m_property->NextSiblingElement();

	if (NULL == Users)
	{
		pantheios::log_ERROR("[InitXmlParse] ", filename, "Users NULL");
		return;
	}




	bool bfound = false;
	TiXmlElement* UserElement = 0;
	for( UserElement = Users->FirstChildElement(); UserElement; UserElement = UserElement->NextSiblingElement() )
	{
		TiXmlAttribute* attributeOfUser = UserElement->FirstAttribute();// user name

		//printf("Name:%s, Value:%s\n", attributeOfUser->Name(), attributeOfUser->Value());

		if (0 == strcmp(attributeOfUser->Value(), gUser))
		{
			bfound = true;
			break;;
		}

	}

	if (false == bfound)
	{
		pantheios::log_ERROR("[InitXmlParse] ", filename, "--Could not found:", gUser);
		return;
	}
	// ---------------------------------------------------------------------------------------------------------------


	// ----------------------3.Set m_readdataElement---------------------------------------------------------------
	
	m_user = UserElement;

	//m_user_back = UserElement;
	
	/*m_first_readwritedataElement = UserElement->FirstChildElement();
	m_readdataElement = UserElement->FirstChildElement();
	m_writedataElement = UserElement->FirstChildElement();
	*/
	// ---------------------------------------------------------------------------------------------------------------

	m_bvalid = true;
}

void QtStraXml::InitRowAndColCnt()
{
	

	if (false == m_bvalid)
		return;

	m_rowcnt = 0;
	m_colcnt = 0;


	TiXmlElement* ele = m_user->FirstChildElement();


	if (ele)
	{
		while (NULL != ele)
		{
			m_rowcnt++;

			TiXmlAttribute* attributeOfdata = ele->FirstAttribute();
			m_colcnt = 0;

			while (attributeOfdata)
			{
				m_colcnt++;
				attributeOfdata = attributeOfdata->Next();
			}

			ele = ele->NextSiblingElement();
		}
	}
	

}

bool QtStraXml::ReadXml(int rowcnt, vector < pair<char*, char*> >& attriRow)
{
	if (false == m_bvalid)
		return false;


	TiXmlElement* ele = m_user->FirstChildElement();


	for (int i = 0; i < rowcnt; i++)
	{
		ele = ele->NextSiblingElement();
		if (NULL == ele)
		{
			pantheios::log_NOTICE("[ReadXml] ele NULL");
			m_bvalid = false;
			return false;
		}
	}




	TiXmlAttribute* attributeOfdata = ele->FirstAttribute();


	while (attributeOfdata)
	{
		char* name = (char*)attributeOfdata->Name();

		char* cvalue = (char*)attributeOfdata->Value();

		attriRow.push_back(make_pair(name, cvalue));


		attributeOfdata = attributeOfdata->Next();

	}


	

	return true;
}




bool QtStraXml::WriteXml(vector < pair<char*, char*> >& attriRow)
{
		
	// ----------------------2.set attribute of data--------------------------------------------------

	TiXmlElement ele( "data" );

	vector < pair<char*, char*> >::iterator it;

	for(it = attriRow.begin(); it != attriRow.end(); it ++)
	{
		ele.SetAttribute( it->first, it->second);
	}

	// -----------------------------------------------------------------------------------------------

	// ----------------------3.save to file--------------------------------------------------

	//m_writedataElement = m_first_readwritedataElement;

	m_user->InsertEndChild( ele );

	
	// -----------------------------------------------------------------------------------------------


	// ----------------------4.reset RowAndColCnt--------------------------------------------------

	InitRowAndColCnt();

	// -----------------------------------------------------------------------------------------------


	return true;
}

bool QtStraXml::ClearXml()
{
	if (false == m_bvalid)
		return false;

	;
	TiXmlElement* ele = m_user->FirstChildElement();



	while (ele)
	{
		m_user->RemoveChild( ele );

		ele = m_user->FirstChildElement();

	}


	// ----------------------4.reset RowAndColCnt--------------------------------------------------

	InitRowAndColCnt();

	// -----------------------------------------------------------------------------------------------


	return true;
}

void QtStraXml::SaveFile()
{
	m_doc->SaveFile();

}

bool QtStraXml::GetColumnName(vector <char*>& attriRow)
{
	if (false == m_bvalid)
		return false;


	
	TiXmlAttribute* attributeOfdata = m_property->FirstAttribute();

	while (attributeOfdata)
	{
		char* cvalue = (char*)attributeOfdata->Value();

		attriRow.push_back(cvalue);


		attributeOfdata = attributeOfdata->Next();

	}


	return true;
}


bool QtStraXml::GetRowAndColCnt(int& rowcnt, int& colcnt)
{
	if (m_bvalid)
	{
		rowcnt = m_rowcnt;

		colcnt = m_colcnt;

		return true;
	}
	

	return false;
}